﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using TBM.Filters;
using TBM.Attributes;

namespace TBM.Controllers
{
    public partial class MailController : MasterController
    {
        [Description("邮件阅读")]
        [TBMAuthorize("邮件管理", "邮件阅读")]
        public ActionResult Reader(long mid, string mailbox)
        {
            ViewBag.MID = mid;
            ViewBag.MailFromMe = false;

            cmd.CommandText = "p_ReadMail";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@MID", SqlDbType.BigInt);
            cmd.Parameters.Add("@Own", SqlDbType.VarChar, 15);
            cmd.Parameters.Add("@Type", SqlDbType.Int);
            cmd.Parameters.Add("@Return", SqlDbType.Int);
            cmd.Parameters["@Return"].Direction = ParameterDirection.ReturnValue;
            cmd.Parameters["@MID"].Value = mid;
            cmd.Parameters["@Own"].Value = CurrUser.UID;

            switch (mailbox)
            {
                case "inbox":
                    cmd.Parameters["@Type"].Value = 1;
                    break;
                case "send":
                    cmd.Parameters["@Type"].Value = 2;
                    break;
                case "draft":
                    cmd.Parameters["@Type"].Value = 3;
                    break;
                case "trash":
                    cmd.Parameters["@Type"].Value = 4;
                    break;
            }

            Models.Mail mail = new Models.Mail();

            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();

                da.Fill(ds);

                DataTable dtMailContext = ds.Tables[0];
                DataTable dtMailFrom = ds.Tables[1];
                DataTable dtMailTo = ds.Tables[2];

                if (dtMailContext.Rows.Count > 0)
                {
                    DataRow row = dtMailContext.Rows[0];
                    mail.ID = (long)row["ID"];
                    mail.Subject = row["Subject"].ToString();
                    mail.Context = row["Context"].ToString();
                    mail.CreateDate = Convert.ToDateTime(row["CreateDate"]);

                    if (dtMailFrom.Rows.Count > 0)
                    {
                        DataRow r = dtMailFrom.Rows[0];
                        mail.MailFrom = new Models.UserListItem
                        {
                            UID = r["UID"].ToString(),
                            FullName = r["FullName"].ToString(),
                            PicUrl = r["PicUrl"] != DBNull.Value ? r["PicUrl"].ToString() + "small.png?" + DateTime.Now.Ticks : Core.Config.DefaultPic
                        };
                    }

                    mail.MailTo = new List<Models.UserListItem>();
                    foreach (DataRow mailto in dtMailTo.Rows)
                    {
                        mail.MailTo.Add(new Models.UserListItem
                        {
                            UID = mailto["UID"].ToString(),
                            FullName = mailto["FullName"].ToString(),
                            PicUrl = mailto["PicUrl"] != DBNull.Value ? mailto["PicUrl"].ToString() + "small.png?" + DateTime.Now.Ticks : Core.Config.DefaultPic
                        });
                    }
                    ViewBag.FindMail = true;
                    ViewBag.MailFromMe = mail.MailFrom.UID == CurrUser.UID;
                }
                else
                {
                    ViewBag.FindMail = false;
                }
            }

            ViewBag.Mail = mail;
            ViewBag.MailBox = mailbox;
            ViewBag.SetToRead = (int)cmd.Parameters["@Return"].Value == 1;

            return PartialView();
        }
    }
}
